package com.zzl.rabbitmq.ack;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zzl.rabbitmq.utils.RabbitMqUtils;
import com.zzl.rabbitmq.utils.SleepUtils;

/**
 * Title: Work03.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/7 13:45 <br>
 *
 * @author zhuangzhuangliang
 */
public class Work04 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("处理时间长。。。");
        //声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, Boolean.FALSE, null);
        //回调函数
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //接收到的消息
            String msg = new String(message.getBody(), "UTF-8");
            SleepUtils.sleep(20);
            System.out.println("接收到消息： "+msg);
            //1.消息的tag 2. true 批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        //设置prefetchCount 预取值
        channel.basicQos(3);
        //关闭自动应答
        channel.basicConsume(QUEUE_NAME,false,deliverCallback, consumerTag ->{
        });
    }
}
